package com.qsmx.core.webfile;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;

import com.qsmx.core.fusion.FusionCode;
import com.qsmx.core.webfile.chromeclient.WebChromeClientImpl;
import com.qsmx.core.webfile.client.WebViewClientImpl;
import com.qsmx.core.webfile.route.RouteKeys;
import com.qsmx.core.webfile.route.Router;

/**
 * @ClassName: WebDelegateImpl
 * @Description: java类作用描述
 * @Author: Jove.Nan
 * @Date: 2019/3/22 5:10 PM
 */
public class WebDelegateImpl extends WebDelegate {

    private IPageLoadListener mIPageLoadListener = null;

    public static WebDelegateImpl create(String url) {
        final Bundle args = new Bundle();
        args.putString(RouteKeys.URL.name(), url);
        final WebDelegateImpl delegate = new WebDelegateImpl();
        delegate.setArguments(args);
        return delegate;
    }

    public static WebDelegateImpl create(String url, String from) {
        final Bundle args = new Bundle();
        args.putString(RouteKeys.URL.name(), url);
        args.putString(FusionCode.WEB_FROM, from);
        final WebDelegateImpl delegate = new WebDelegateImpl();
        delegate.setArguments(args);
        return delegate;
    }

    public static WebDelegateImpl create(String url, String from, boolean isHasTitle) {
        final Bundle args = new Bundle();
        args.putString(RouteKeys.URL.name(), url);
        args.putString(FusionCode.WEB_FROM, from);
        args.putBoolean(FusionCode.WEB_HAS_NO_TITLE, isHasTitle);
        final WebDelegateImpl delegate = new WebDelegateImpl();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return getWebView();
    }

    public void setPageLoadListener(IPageLoadListener listener) {
        this.mIPageLoadListener = listener;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        if (getUrl() != null) {
            //用原生方式模拟Web跳转
            Router.getInstance().loadPage(this, getUrl());
        }
    }

    @Override
    public IWebViewInitializer setInitializer() {
        return this;
    }


    @Override
    public FileWebView initWebView(FileWebView webView, Context context) {
        return new WebViewInitializer().createWebView(webView, context);
    }

    @Override
    public FileWebViewClient initWebViewClient() {
        final WebViewClientImpl client = new WebViewClientImpl(this);
        client.setPageLoadListener(mIPageLoadListener);
        return client;
    }

    @Override
    public FileWebChromeClient initWebChromeClient() {
        return new WebChromeClientImpl();
    }
}
